package com.zwq.springbootlearn.service;

import com.zwq.springbootlearn.enums.ResultEnum;
import com.zwq.springbootlearn.exception.GirlException;
import com.zwq.springbootlearn.model.Girl;
import com.zwq.springbootlearn.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: zwq
 * Create in 2018/6/30 14:24
 */
@Service
public class GirlService {

    @Autowired
    private GirlRepository girlRepository;

    //spring 事务管理
    @Transactional
    public void insertTwo(){
        Girl girlA= new Girl();
        girlA.setCupSize("A");
        girlA.setAge(18);
        girlRepository.save(girlA);

        Girl girlB= new Girl();
        girlB.setCupSize("C");
        girlB.setAge(19);
        girlRepository.save(girlB);
    }

    public void getAge(Integer id) throws Exception{
        Girl girl = girlRepository.getOne(id);
        Integer age = girl.getAge();
        if(age<=10){
            //返回 "你还在上小学吧"
            throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
        }else if(age<=16){
            //返回 "你可能还在上初中"
            throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
        }else{
            //其他异常
            throw new Exception("其他错误");
        }
    }

    //根据Id查找一个女生
    public Girl findOne(Integer id){
        return girlRepository.findById(id).get();
    }
}
